// component/Vast-module/@weixin/AgreePrivacy/index.js
const App = getApp()
Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
    // 隐私授权模式：主动模式-initiative（使用 wx.getPrivacySetting）；被动触发-passive（使用 wx.onNeedPrivacyAuthorization）；
    authorizeType:{
      type: String,
      value: "initiative"
    },
    miniAppName: String,
    showDialog: Boolean,
    title: String,
    desc: String,
    privacyContractName: String,
		content: String,
		
		top:{
			type: String,
			value: "320rpx"
		},

    showCustom_privacyContract: {
      type: Boolean,
      value: false
    },
    policyConect: {
      type: Array,
      value: App.globalData.policyConect
    },
    
  
	},

	/**
	 * 组件的初始数据
	 */
	data: {
    showDialog: false,

  },
  
  lifetimes: {
    attached: function() {

      // console.log('policyConect隐私条款中涉及的作用，在App.js中定义',this.data.policyConect)
      var authorizeType = this.data.authorizeType

      if(authorizeType == 'initiative'){
        if (wx.getPrivacySetting) {
          // 主动查询授权时，是否显示弹框的变量showDialog不用页面的子组件传递，而是默认false，当查询到需要授权时,设置为true显示弹框
          this.setData({showDialog: false})   

          wx.getPrivacySetting({
            success: res => {
                // console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
                if (res.needAuthorization) {
                  this.setData({privacyContractName: res.privacyContractName})
                  this.showDialog()
                } else{
                  this.triggerEvent("agree")
                }
            },
            fail: () => { },
            complete: () => { },
          })
        } else {
          // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用

          // console.log("低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用")
          this.triggerEvent("agree")
        }
      }
    },
  },

	/**
	 * 组件的方法列表
	 */
	methods: {

    /** authorizeType == 'initiative 或 passive' 点击拒绝授权 */ 
    handleDisagree(e) {
      // console.log(e)
      var authorizeType = this.data.authorizeType
      // var paramsObj= {buttonId: 'disagree-btn', event: 'disagree',authorizeType: authorizeType}
      var paramsObj= {buttonId: e.currentTarget.id, event: 'disagree',authorizeType: authorizeType}
      var miniAppName = this.data.miniAppName
      wx.showModal({
        title: `再次确认`,
        content: `您是否确认拒绝对[${miniAppName}]小程序，进行个人隐私数据授权？点击[是]确认拒绝授权，点击[否]返回。`,
        cancelText:"否",  
          confirmText:"是",    
        complete: (res) => {
          // 点击返回，不确认
          if (res.cancel) {
            return;
          }
          // 点击确定，执行拒绝隐私授权
          if (res.confirm) {
            this.triggerEvent("disagree",paramsObj)
            this.disShowDialog()
          }
        }
      })
    },


    /** authorizeType == 'initiative' 同意并授权 */  
    handleAgree(e) {
      // console.log('同意并授权',e)
      var  paramsObj = {authorizeType: 'initiative'}
      this.triggerEvent("agree",paramsObj)
      this.disShowDialog()
    },
    /** authorizeType == 'passive' 同意授权 */
    handleAgreePrivacyAuthorization(e) {
      // console.log('同意并授权',e)
      // var paramsObj= {buttonId: 'agree-btn', event: 'agree',authorizeType: 'passive'}
      var paramsObj= {buttonId: e.currentTarget.id, event: 'agree',authorizeType: 'passive'}
      this.triggerEvent("agree",paramsObj)
    },





    /** 显示弹框 */
    showDialog() {
        this.setData({
          showDialog: true
        })
    },
    /** 关闭弹框 */
    disShowDialog() {
        this.setData({
          showDialog: false
        })
    },

    /** 打开隐私协议内容 */
    openPrivacyContract(e) {
      var authorizeType = e.currentTarget.dataset.authorizetype
      if( authorizeType=='initiative' ){
        wx.openPrivacyContract({
          success: res => {
            // console.log('openPrivacyContract success')
          },
          fail: res => {
            // console.error('openPrivacyContract fail', res)
          }
        })
      }else if( authorizeType=='passive' ){
        this.setData({
          showCustom_privacyContract: true
        })
      }
    },
    // 关闭自定义隐私信息授权协议
    closePrivacyContract(){
      this.setData({
        showCustom_privacyContract: false
      })
    }
  }
})
